﻿using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKCodeBase.WinForm;
using VerticalMenu.Utility;

namespace VerticalMenu.Commands
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class ExternalCommandShowPage : IExternalCommand, IExternalCommandAvailability
	{
		public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			try
			{
				int num = Convert.ToInt32(commandData.Application.Application.VersionNumber);
				if (num < 2015)
				{
					MessageBox.Show("此功能只在Revit2015及其以上版本可用", "盈建科提示");
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				if (null == ExternalCommandShowPage._hWndRevit)
				{
					Process currentProcess = Process.GetCurrentProcess();
					IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
					ExternalCommandShowPage._hWndRevit = new WindowHandle(mainWindowHandle);
				}
				VerticalMenuApplication.m_ThisApp.SetWindowVisibility(commandData, true);
				StateController.SaveState(StateController.StateEnum.SE_Show);
				this.SetKeyboardShortcuts(commandData.Application.Application.VersionName);
				int wMsg = 7;
				ExternalCommandShowPage.SendMessage(ExternalCommandShowPage._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			catch (Exception)
			{
				MessageBox.Show("竖向菜单未完成注册", "盈建科提示");
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
		{
			return applicationData.ActiveUIDocument != null;
		}

		private void SetKeyboardShortcuts(string versionName)
		{
			try
			{
				string path = Environment.ExpandEnvironmentVariables("%appdata%\\Autodesk\\Revit");
				string path2 = Path.Combine(path, versionName);
				string text = Path.Combine(path2, "KeyboardShortcuts.xml");
				if (File.Exists(text) && VerticalMenuApplication.m_CommandTableInfos != null)
				{
					XmlDocument xmlDocument = new XmlDocument();
					xmlDocument.Load(text);
					string commandName = "";
					string ribbonTabName = "";
					string ribbonPanelName = "";
					VerticalMenuApplication.m_CommandTableInfos.GetCmdTabAndPanelName("显示菜单", ref commandName, ref ribbonTabName, ref ribbonPanelName);
					this.SetOneKeyboardShortcuts(xmlDocument, commandName, "SHO", ribbonTabName, ribbonPanelName);
					VerticalMenuApplication.m_CommandTableInfos.GetCmdTabAndPanelName("隐藏菜单", ref commandName, ref ribbonTabName, ref ribbonPanelName);
					this.SetOneKeyboardShortcuts(xmlDocument, commandName, "HID", ribbonTabName, ribbonPanelName);
					xmlDocument.Save(text);
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private void SetOneKeyboardShortcuts(XmlDocument doc, string commandName, string shortcuts, string ribbonTabName, string ribbonPanelName)
		{
			XmlElement documentElement = doc.DocumentElement;
			string text = commandName.Replace("\\r", "");
			text = text.Replace("\\n", "");
			string text2 = string.Concat(new string[]
			{
				"CustomCtrl_%CustomCtrl_%",
				ribbonTabName,
				"%",
				ribbonPanelName,
				"%",
				text
			});
			string text3 = ribbonTabName + "&gt;" + ribbonPanelName;
			string xpath = string.Format("Shortcuts/ShortcutItem[@CommandName='{0}']", commandName);
			XmlNode xmlNode = doc.SelectSingleNode(xpath);
			if (xmlNode == null)
			{
				XmlElement xmlElement = doc.CreateElement("ShortcutItem");
				xmlElement.SetAttribute("CommandName", commandName);
				xmlElement.SetAttribute("CommandId", text2);
				xmlElement.SetAttribute("Shortcuts", shortcuts);
				xmlElement.SetAttribute("Paths", text3);
				documentElement.AppendChild(xmlElement);
			}
			else
			{
				XmlElement xmlElement2 = xmlNode as XmlElement;
				if (!xmlElement2.HasAttribute("Shortcuts"))
				{
					xmlElement2.SetAttribute("Shortcuts", shortcuts);
				}
				else if (xmlElement2.GetAttribute("Shortcuts").CompareTo(shortcuts) != 0)
				{
					xmlElement2.SetAttribute("Shortcuts", shortcuts);
				}
				if (!xmlElement2.HasAttribute("CommandId"))
				{
					xmlElement2.SetAttribute("CommandId", text2);
				}
				else if (xmlElement2.GetAttribute("CommandId").CompareTo(text2) != 0)
				{
					xmlElement2.SetAttribute("CommandId", text2);
				}
				if (!xmlElement2.HasAttribute("Paths"))
				{
					xmlElement2.SetAttribute("Paths", text3);
				}
				else if (xmlElement2.GetAttribute("Paths").CompareTo(text3) != 0)
				{
					xmlElement2.SetAttribute("Paths", text3);
				}
			}
		}

		public static WindowHandle _hWndRevit = null;
	}
}
